#!/usr/bin/python
# -*- coding: UTF-8 -*-

import re
import sys, os
import xml.sax.saxutils

def clear_parenthesis(str):
  return re.sub("\(.*\)", "", title)


titles = set()
synonyms = [] 
for name in os.listdir(sys.argv[1]):
  title = re.match(r"\d+-(.*).page", name).group(1)
  titles.add(title)
  synonyms.append((clear_parenthesis(title).strip(), title))
  f = file(sys.argv[1] + name).read()
  m = re.search("\|\s*прозвища\s*=\s*([^\|\}\{]*)", f, re.U | re.M)
  if m and m.group(1).strip():
    field = m.group(1).strip()
    field = xml.sax.saxutils.unescape(field)
    field = re.sub(r"\<.*\>|\(.*\)|\[.*\]|\{.*\}|'|\<.*$|\(.*$|\[.*$|\{.*$|«|»", "", field)
    nicks = field.split(',')
    for nick in nicks:
      synonyms.append((nick.strip(), title))

for line in file(sys.argv[2]):
  cells = line.split('\t')
  if len(cells) < 3:
    continue
  m = re.match(r"#.*\[\[(.*)\]\]", cells[2])
  if not m:
    continue
  title = m.group(1)
  synonym = cells[1]
  if title in titles:
    synonyms.append((clear_parenthesis(synonym).strip(), title))

for synonym, title in synonyms:
  print "%s|%s" % (synonym, title)


